package argo.hot100.search2mid;

public class FindMin {


    public int findMin(int[] nums) {
        int low = 0;
        int high = nums.length - 1;
        while (low < high) {
            int pivot = low + (high - low) / 2;
            if (nums[pivot] < nums[high]) {
                high = pivot;
            } else {
                low = pivot + 1;
            }
        }
        return nums[low];
    }


    public int findMinSlf(int[] nums) {
        int min = -1, left = 0, right = nums.length - 1, mid;
        if (nums.length == 1) {
            return nums[0];
        }

        if (nums[left] <= nums[right]) {
            return nums[left];
        } else if (nums[left] > nums[right] && nums[right - 1] > nums[right]) {
            return nums[right];
        } else {

            while (left <= right) {
                mid = (left + right) / 2;
                if (nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]) {
                    return nums[mid];
                }

                if (nums[left] <= nums[mid] && nums[left] >= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
        }
        return min;


    }
}
